package com.pipe.protocol.login;

import com.pipe.util.IntegerConveter;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;

/**
 * @author Kiven
 */
public class ComBinlogDumpPackage {
    private byte status = 0x12;
    private byte[] binlogPos;
    private byte[] flags;
    private byte[] serverId;
    private byte[] binlogFileName;

    public ComBinlogDumpPackage(int binlogPos, short flags, int serverId, String binlogFileName) {
        this.binlogPos = IntegerConveter.getByte4Array(binlogPos);
        this.flags = IntegerConveter.getByte2ArrayFromShort(flags);
        this.serverId = IntegerConveter.getByte4Array(serverId);
        this.binlogFileName = binlogFileName.getBytes();
    }

    public static ComBinlogDumpPackage build(int binlogPos, short flags, int serverId, String binlogFileName){
        return new ComBinlogDumpPackage(binlogPos, flags, serverId, binlogFileName);
    }

    public ByteBuf toBytebuf(){
        ByteBuf byteBuf = Unpooled.directBuffer(1 + binlogPos.length + flags.length + serverId.length + binlogFileName.length);
        byteBuf.writeByte(status);
        byteBuf.writeBytes(binlogPos);
        byteBuf.writeBytes(flags);
        byteBuf.writeBytes(serverId);
        byteBuf.writeBytes(binlogFileName);
        return byteBuf;
    }
}